<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">补货编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" class="layui-input" type="text" placeholder="补货编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <!--<div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="simpleWorkOrderBtnAdd" class="layui-btn icon-btn">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>-->
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="replenishmentOrderTable" lay-filter="replenishmentOrderTable"></table>
        </div>
    </div>
</div>
<style>
    .left-btn{
        flex: 1;
        text-align: left;
    }
    .flex-between{
        width: calc(100% - 40px);
        display: flex;
        justify-content: space-between;
    }
    .productionWorkOrderAddForm .layui-form-item .layui-input-inline{
        width: 290px;
    }
    .productionWorkOrderAddForm .w-auto{
        width: 100px !important;
    }
    .selectProductDetail .layui-table-cell {
        height: auto;
    }
    body .layui-table tbody tr.disabledBg{
        background-color: #F5F5F5 !important;
    }
    .layui-table-page-left{
        text-align: left;
    }
    .scroll-table-5-4 .layui-table-body{
        max-height: 535px;
    }
    .scroll-table-4-5 .layui-table-body{
        max-height: 490px;
    }
</style>
<!-- 表格操作列 -->
<script type="text/html" id="replenishmentOrderTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="details">详情</a>
    {{# if(d.status == "stayAudit"){  }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="audit">审核</a>
    {{# } }}
</script>

<!--详情-->
<script type="text/html" id="detailsTemplate">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <!-- 表格 -->
                <table class="layui-table" id="detailsTable"
                       lay-filter="detailsTable"></table>
                <table class="layui-table" id="replenishmentOrderDetailsTable"
                       lay-filter="replenishmentOrderDetailsTable"></table>

            </div>
        </div>
    </div>
</script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#replenishmentOrderTable',
            url: config.base + 'enterprise/purchase/admin/replenishmentOrder/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                ,{field: 'code', title: '补货订单编号'}
                ,{field: 'skuCount', sort: true, title: '补货物料数量'}
                ,{field: 'alarmStock', sort: true, title: '建议补货数量'}
                ,{field: 'totalAmount', title: '订单金额'}
                ,{field: 'statusDesc', title: '状态'}
                ,{field: 'creatorName', title: '补货人'}
                ,{field: 'createTime', sort: true, title: '创建时间'}
                ,{align: 'center', toolbar: '#replenishmentOrderTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(replenishmentOrderTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'details') { //详情
                showDataStatement(data);
            } else if (obj.event === 'audit') { // 审核
                doDelete(data);
            }
        });

        //监听排序
        table.on('sort(replenishmentOrderTable)', function(obj){
            table.reload('replenishmentOrderTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#replenishmentOrderBtnAdd').click(function () {
            showEditModel();
        });

        function showDataStatement(data) {
            admin.open({
                type: 1,
                area: ['82%', '840px'],
                offset: '65px',
                title: '查看工单报表',
                content: $('#detailsTemplate').html(),
                success: function () {
                    //工单
                    let orderDetails = [];
                    orderDetails.push(data);
                    var detailsTable = table.render({
                        elem: '#detailsTable',
                        page: false,
                        data: orderDetails,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers', title: '序号'}
                            ,{field: 'code', title: '补货订单编号'}
                            ,{field: 'skuCount', sort: true, title: '补货物料数量'}
                            ,{field: 'alarmStock', sort: true, title: '建议补货数量'}
                            ,{field: 'totalAmount', title: '订单金额'}
                            ,{field: 'statusDesc', title: '状态'}
                            ,{field: 'creatorName', title: '补货人'}
                            ,{field: 'createTime', sort: true, title: '创建时间'}
                        ]],
                        done: function (res, curr, count) {
                        }
                    });

                    // 渲染表格
                    var replenishmentOrderDetailsTb = table.render({
                        elem: '#replenishmentOrderDetailsTable',
                        url: config.base + 'enterprise/purchase/admin/replenishmentOrderDetail/list?replenishmentOrderId=' + data.id,
                        page: true,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers', title: '序号'}
                            , {field: 'productPic', title: 'sku图片', width: 100,templet:'<div><img src="{{ d.productPic}}" style="height: 80px;width: 80px;"></div>'}
                            , {field: 'skuCode', title: '编码'}
                            , {field: 'productName', title: '物料名称'}
                            , {field: 'skuSpecValueNames',title: '规格码'}
                            , {field: 'curentStock', title: '当前库存'}
                            , {field: 'planReplenishment', title: '建议补货数量'}
                            /*, {field: 'creatorName', sort: true, title: '添加人'}
                            , {field: 'createTime', sort: true, title: '发货时间'}
                            , {align: 'center', toolbar: '#inventoryInoutTableBar', title: '操作', width: 100}*/
                        ]],
                        done: function (res, curr, count) {
                        }
                    });
                    // 工具条点击事件
                }
            });
        }

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '680px',
                offset: '65px',
                title: data ? '修改补货订单表' : '添加补货订单表',
                content: $('#replenishmentOrderForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='status']").find("option");
                        layui.each(option, function(index, item){
                            if(data.status  == item.text){
                                data.status = item.value;
                            }
                        });
                    }

                    form.val('replenishmentOrderForm', data);
                    // 表单提交事件
                    form.on('submit(replenishmentOrderFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/purchase/admin/replenishmentOrder/update' : 'enterprise/purchase/admin/replenishmentOrder/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('replenishmentOrderTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(data) {
            layer.confirm('确定要审核通过吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/purchase/admin/replenishmentOrder/update', {id:data.id,status:"success"}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        table.reload('replenishmentOrderTable');
                        layer.closeAll('page');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'PUT');
            });
        }

        // 导出excel
        $('#replenishmentOrderBtnExport').click(function () {
            var checkRows = table.checkStatus('replenishmentOrderTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>